<?php
namespace App\Http\Services;
use GuzzleHttp\Client;
class APIHelper
{
    private $client = null;
    /**
     * post用
     * @param $baseUri
     * @param $body
     * @param $apiStr
     * @return string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function post($baseUri, $body, $apiStr)
    {
        $client = new Client(['base_uri' => $baseUri]);
        $res = $client->request('POST', $apiStr,
            ['json' => $body,
                'headers' => [
                    'Content-type'=> 'application/json',
//                'Cookie'=> 'XDEBUG_SESSION=PHPSTORM',
                    "Accept"=>"application/json"]
            ]);
        $data = $res->getBody()->getContents();

        return $data;
    }

    /**
     * get用
     * @param $baseUri
     * @param $apiStr
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function get($baseUri, $url, $apiStr)
    {
        $this->client = new Client([
            'base_uri' => $baseUri,
            'timeout'  => 10.0
        ]);
        $response = $this->client->get($url.'?zipcode='.$apiStr);
        $body = $response->getBody();
        $data = json_decode($body, true);
        return $data;
    }

}